package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.BizEnum;
import com.qk.common.exception.BizException;
import com.qk.dto.dept.AddDeptDTO;
import com.qk.dto.dept.QueryPageDeptDTO;
import com.qk.dto.dept.UpdateDeptDTO;
import com.qk.entity.Dept;
import com.qk.management.mapper.DeptMapper;
import com.qk.management.service.DeptService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @Author:gzc
 * @Date: 2025/09/29 21:06
 * @Description:
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {


    @Override
    public void addDept(AddDeptDTO addDeptDTO) {
        boolean hasNull = BeanUtil.hasNullField(addDeptDTO);
        if (hasNull) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Dept dept = BeanUtil.copyProperties(addDeptDTO, Dept.class);
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
//        deptMapper.addDept(dept);
        this.save(dept);
    }


    @Override
    public PageResult<Dept> queryPage(QueryPageDeptDTO queryPageDeptDTO) {
//        PageInfo<Dept> pageInfo = PageHelper.startPage(deptQueryListByPageDto.getPage(), deptQueryListByPageDto.getPageSize())
//                .doSelectPageInfo(() -> deptMapper.queryPage(deptQueryListByPageDto));
//        return PageResult.<Dept>builder().total(pageInfo.getTotal()).rows(pageInfo.getList()).build();
//        Page<Dept> page = new Page<>(deptDTO.getPage(), deptDTO.getPageSize());
//        LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.apply(StringUtils.hasText(deptDTO.getName()),
//                        "replace(dept.name,' ','') like concat ('%',replace({0},' ',''),'%')", deptDTO.getName())
//                .eq(deptDTO.getStatus() != null, Dept::getStatus, deptDTO.getStatus())
//                .orderByDesc(Dept::getUpdateTime);
//        IPage<Dept> iPage = deptMapper.selectPage(page, queryWrapper);
        Page<Dept> page = this.page(new Page<>(queryPageDeptDTO.getPage(), queryPageDeptDTO.getPageSize()), Wrappers.lambdaQuery(Dept.class)
                .apply(StringUtils.hasText(queryPageDeptDTO.getName()), "replace(dept.name,' ','') like concat ('%',replace({0},' ',''),'%')", queryPageDeptDTO.getName())
                .eq(queryPageDeptDTO.getStatus() != null, Dept::getStatus, queryPageDeptDTO.getStatus())
                .orderByDesc(Dept::getUpdateTime));
        return PageResult.<Dept>builder().total(page.getTotal()).rows(page.getRecords()).build();
    }

    @Override
    public Dept queryById(Integer id) {
        if (Objects.isNull(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
//        Dept dept = deptMapper.queryById(id);
        return this.getById(id);
    }

    @Override
    public void updateDept(UpdateDeptDTO updateDeptDTO) {
        boolean hasNull = BeanUtil.hasNullField(updateDeptDTO);
        if (hasNull) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Dept dept = BeanUtil.copyProperties(updateDeptDTO, Dept.class);
        dept.setUpdateTime(LocalDateTime.now());
//        deptMapper.updateDept(dept);
        this.updateById(dept);
    }


    @Override
    public void deleteDept(Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
//        deptMapper.deleteDept(id);
        this.removeById(id);
    }

    @Override
    public List<Dept> queryAll() {
//        List<Dept> deptsList = deptMapper.queryAll();
        return this.baseMapper.selectList(null);
    }
}
